/* eslint-disable default-case */
import { message } from 'antd';
import {
  getList,
  markCharge,
  getSuppDataList,
  getCustList,
  getSendOrdSndr,
  getToVoid,
  getOnceDataList,
  getNsOnceAddDataList,
  getProductListData,
  downloadFile,
  getCustNameListData,
  uploadFileDataList,
  addOnceDataList,
  deleteRecordData,
  modalData,
  getDetail,
  getDetailListData,
  getUpdateDetailData,
  updateSubmitData,
  getEmpNameList,
  uploadFileCharge,
} from '@/services/once';
import * as service from '@/services/socket';

export default {
  state: {
    formFieldsValue: {},
    currentPage: 1,
    activeKey: '1',
    expandForm: false,
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    data: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    custDataList: {
      // 新增列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },

    empDetailList: {
      // 详情雇员明细列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    empNameList: [],
    batchQuery: {},
    currentStep: 0,
    showProgress: false,
    progressPercent: 0,
    resultInfo: {
      // 结果数据必传
      batchName: '', // 任务名称
      taskCount: 0, // 导入数据
      successCount: 0, // 成功数据
      errorCount: 0, // 失败数据
      batchNo: '', // 批次号
      errorUrl: '/csApi/oncefeeFile/dowoloadEmpDet', // 下载错误信息地址
      errorParams: null, // 下载错误数据参数
    },
    valuesArr: [
      'ordSndrId',
      'feeSta',
      'custId',
      'createTime',
      'creName',
    ]
  },
  effects: {
    *markCharge({ payload }, { call }) {
      const res = yield call(markCharge, payload);
      return res;
    },
    *getList({ payload }, { call, put }) {
      const res = yield call(getList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (res.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message);
      }
      return res;
    },

    *getOnceList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;

      yield put({
        type: 'getIndexList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getOnceDataList, payload);
      if (response.success) {
        yield put({
          type: 'getIndexList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表失败');
      }
    },

    // 获取签约方下拉
    *getSuppList({ payload }, { call }) {
      const res = yield call(getSuppDataList, payload);
      return res;
    },

    // 获取客户下拉
    *getCustListData({ payload }, { call }) {
      const res = yield call(getCustList, payload);
      return res;
    },

    // 发送签约方
    *sendSupper({ payload }, { call }) {
      const res = yield call(getSendOrdSndr, payload);
      return res;
    },

    // 作废
    *toVoid({ payload }, { call }) {
      const res = yield call(getToVoid, payload);
      return res;
    },

    // 全国收费一次性-新增的列表
    *getListData({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;

      yield put({
        type: 'getAddListData',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getNsOnceAddDataList, payload);
      if (response.success) {
        yield put({
          type: 'getAddListData',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表失败');
      }
    },

    // 获取产品下拉
    *getProductList({ payload }, { call }) {
      const res = yield call(getProductListData, payload);
      return res;
    },

    // 下载模板
    *download({ payload }, { call }) {
      const response = yield call(downloadFile, payload);
      return response;
    },

    // 根据签约方获取客户名称
    *getcustList({ payload }, { call }) {
      const res = yield call(getCustNameListData, payload);
      return res;
    },

    // 员工明细上传
    *uploadFileList({ payload }, { call }) {
      const res = yield call(uploadFileDataList, payload);
      return res;
    },

    // 收费一次性 新增 提交
    *addOnceData({ payload }, { call }) {
      const res = yield call(addOnceDataList, payload);
      return res;
    },

    // 删除某个员工明细
    *deleteRecord({ payload }, { call }) {
      const res = yield call(deleteRecordData, payload);
      return res;
    },

    // 新增修改模态框提交数据
    *modalSubmitData({ payload }, { call }) {
      const res = yield call(modalData, payload);
      return res;
    },

    // 收费一次性查看详情
    *getDetailData({ payload }, { call }) {
      const res = yield call(getDetail, payload);
      return res;
    },

    // 收费一次性查看详情 - 雇员明细
    *getDetailList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getEmpDetailList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getDetailListData, payload);
      if (response.success) {
        yield put({
          type: 'getEmpDetailList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表失败');
      }
    },

    // 修改前查看详情
    *getUpdateDetail({ payload }, { call }) {
      const res = yield call(getUpdateDetailData, payload);
      return res;
    },

    // 修改提交
    *updateSubmit({ payload }, { call }) {
      const res = yield call(updateSubmitData, payload);
      return res;
    },
    // 单个调整：输入姓名,证件号模糊查询
    *fetchEmpNameList({ payload }, { call, put }) {
      const response = yield call(getEmpNameList, payload);
      yield put({
        type: 'updateModelData',
        payload: { empNameList: [] },
      });
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { empNameList: response.data },
        });
        return response.data;
      }
      return true;
    },
    // 获取下载模板fileId fileName
    *getDownloadInfo({ payload }, { call, put }) {
      const response = yield call(downloadFile, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      }
    },
    // 上传文件:批量进度
    *uploadFileWithProgress({ payload }, { call, put }) {
      const response = yield call(uploadFileCharge, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            batchId: response.data && response.data.batchId,
            progressPercent: 0,
            onceFeeChrgDrafId: response.data && response.data.onceFeeChrgDrafId,
          },
        });
        if (response.data && response.data.successProp !== 100) {
          yield call(service.socketChannel, {
            type: 'sub',
            data: { to: response.data && (response.data.batchId || response.data), type: 'task' },
          });
        } else {
          yield put({
            type: 'updateBatchTask',
            payload: response.data,
          });
        }
      } else {
        // 上传失败
        message.warning(`${response.message || '上传失败'}`);
      }
      return response;
    },
    // 匹配唯一batchId
    *matchBatchId({ payload }, { put, select }) {
      const { batchId } = yield select(state => state.oncechargens);
      if (batchId === payload.batchId) {
        yield put({
          type: 'updateBatchTask',
          payload,
        });
      }
    },
  },
  reducers: {
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 收费一次性详情 - 雇员明细
    getEmpDetailList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        empDetailList: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 列表数据
    getIndexList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 新增列表
    getAddListData(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        custDataList: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    // 更新数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      let { showProgress, resultInfo, currentStep } = state;
      const { cacheKey } = state;
      showProgress = true;
      if (payload.successProp === 100) {
        resultInfo.errorParams = {
          batchId: payload.batchId,
          cacheKey,
        };
        resultInfo = { ...resultInfo, ...payload };
        currentStep += 1;
        showProgress = false;
      }
      return {
        ...state,
        showProgress,
        progressPercent: payload.successProp,
        resultInfo,
        currentStep,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      return service.listen(action => {
        switch (action.type) {
          case 'task':
            dispatch({
              type: 'matchBatchId',
              payload: action.data,
            });
            // 全局保存
            dispatch({
              type: 'publicdata/updateBatchTaskData',
              payload: action.data,
            });
            break;
        }
      });
    },
  },
};
